package com.bw.zhuangao5___day002.Activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.ashokvarma.bottomnavigation.TextBadgeItem;
import com.bw.mvp.V.BannerActivity;
import com.bw.zhuangao5___day002.Adpter.FragmentAdpter.FragmentAdpterOne;
import com.bw.zhuangao5___day002.Fragment.BlankFragmentBooks;
import com.bw.zhuangao5___day002.Fragment.BlankFragmentGames;
import com.bw.zhuangao5___day002.Fragment.BlankFragmentHome;
import com.bw.zhuangao5___day002.Fragment.BlankFragmentMovies;
import com.bw.zhuangao5___day002.Fragment.BlankFragmentMusic;
import com.bw.zhuangao5___day002.R;
import com.frame.lib_common.slidingmenu.SlidingMenu;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BannerActivity {

    BottomNavigationBar bottomNavigationBar;
    public static ViewPager mainViewPager;
    public static LinearLayout bian;

    /**
     * 该方法用来捕捉手机键盘被按下的事件。
     * @param keyCode  该参数指的是被按下的键的键盘码，手机键盘中每个按钮都会有其对应的键盘码，
     *                 在应用程序都是通过键盘码才知道用户按下的是哪个键。
     * @param event    当用户按下按键时，系统会自动将事件封装成KeyEvent对象供应用程序使用。
     *                 按键事件KeyEvent 按键事件有多种常量类型，比如 KEYCODE_BACK
     * @return
     */
    Long exitTime =0L;

    public static void fanhui() {
        mainViewPager.setCurrentItem(4);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK ){
            //判断用户两次按键的时间差是否在一个预期值之内，是的话直接直接退出，不是的话提示用户再按一次后退键退出。
            if(System.currentTimeMillis() - exitTime > 2000){
                Toast.makeText(this,"在点就退出",Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
                //当返回true时，表示已经完整地处理了这个事件，并不希望其他的回调方法再次进行处理，而当返回false时，
                // 表示并没有完全处理完该事件，更希望其他回调方法继续对其进行处理，
                return true;
            }else{
                finish(); //结束当前activity
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public int bandLayoud() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {

        bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);

        mainViewPager = findViewById(R.id.main_ViewPager);
        bian = findViewById(R.id.bian);
    }

    @Override
    public void initDate() {


        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new BlankFragmentHome());
        fragmentList.add(new BlankFragmentBooks());
        fragmentList.add(new BlankFragmentMusic());
        fragmentList.add(new BlankFragmentMovies());
        fragmentList.add(new BlankFragmentGames());
        FragmentAdpterOne fragmentAdpterOne = new FragmentAdpterOne(getSupportFragmentManager(), FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,fragmentList);
        mainViewPager.setAdapter(fragmentAdpterOne);
onWindowFocusChanged(true);

        TextBadgeItem numberBadgeItem = new TextBadgeItem();
        numberBadgeItem.setText("99") //显示的文本
                .setTextColor("#000000")
                .setBackgroundColor("#D81B60")
                .setGravity(Gravity.RIGHT|Gravity.TOP) //位置，默认右上角
                .setHideOnSelect(true); //true：当选中状态时消失，非选中状态显示,moren false

        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.mipmap.shouye, "Home"))
                .addItem(new BottomNavigationItem(R.mipmap.shipin, "Books"))
                .addItem(new BottomNavigationItem(R.mipmap.fenlei, "Music"))
                .addItem(new BottomNavigationItem(R.mipmap.kefu, "Movies").setBadgeItem(numberBadgeItem))
                .addItem(new BottomNavigationItem(R.mipmap.wode, "Games"))
                .initialise();//所有的设置需在调用该方法前完成


        bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener(){
            @Override
            public void onTabSelected(int position) {
                //未选中->选中
                mainViewPager.setCurrentItem(position);
            }
            @Override
            public void onTabUnselected(int position) {
                //选中->未选中
            }
            @Override
            public void onTabReselected(int position) {
                //选中->选中
            }
        });


    }



    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }


}
